package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherMessagePushExtraMapper;
import com.zjdiepu.www.ibiz.other.IOtherMessagePushExtraService;
import com.zjdiepu.www.model.other.dto.OtherMessagePushExtra;
import com.zjdiepu.www.model.other.ext.OtherMessagePushExtraExt;

/**
 * other_额外消息推送Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2018年01月18日 11时23分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherMessagePushExtraServiceImpl extends BaseService implements IOtherMessagePushExtraService {
	public static final Logger logger = LoggerFactory.getLogger(OtherMessagePushExtraServiceImpl.class);

	@Autowired
	private OtherMessagePushExtraMapper otherMessagePushExtraMapper;
	
	@Override
	public void saveUpdateAndDelete(OtherMessagePushExtra otherMessagePushExtra, OperateType operType) {
		switch (operType) {
		case add:
			otherMessagePushExtra.initInsertData(otherMessagePushExtra.getModifier(), otherMessagePushExtra.getModifierName());
			otherMessagePushExtraMapper.insert(otherMessagePushExtra);
			break;
		case edit:
			otherMessagePushExtra.initUpdateData(otherMessagePushExtra.getModifier(), otherMessagePushExtra.getModifierName());
			// 影响的记录数
			int updateCount = otherMessagePushExtraMapper.updateByPrimaryKeySelective(otherMessagePushExtra);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherMessagePushExtra.initDeleteData(otherMessagePushExtra.getModifier(), otherMessagePushExtra.getModifierName());
			// 影响的记录数
			int deleteCount = otherMessagePushExtraMapper.updateByPrimaryKeySelective(otherMessagePushExtra);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherMessagePushExtra> selectByExt(OtherMessagePushExtraExt otherMessagePushExtraExt) {
		return otherMessagePushExtraMapper.selectByExt(otherMessagePushExtraExt);
	}
	
	@Override
	public int countByExt(OtherMessagePushExtraExt otherMessagePushExtraExt){
		return otherMessagePushExtraMapper.countByExt(otherMessagePushExtraExt);
	}
	
	@Override
	public OtherMessagePushExtra selectById(String id) {
		return otherMessagePushExtraMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherMessagePushExtraMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherMessagePushExtra otherMessagePushExtra) {
		return otherMessagePushExtraMapper.countRefined(otherMessagePushExtra);
	}

	@Override
	public OtherMessagePushExtra selectRefined(OtherMessagePushExtra otherMessagePushExtra) {
		return otherMessagePushExtraMapper.selectRefined(otherMessagePushExtra);
	}
	
	@Override
	public int deletedRefined(OtherMessagePushExtra otherMessagePushExtra) {
		return otherMessagePushExtraMapper.deletedRefined(otherMessagePushExtra);
	}

	@Override
	public boolean repeat(OtherMessagePushExtra otherMessagePushExtra) {
		int count = countRefined(otherMessagePushExtra);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherMessagePushExtra.getId())) {
			return true;
		}
		OtherMessagePushExtra dbItem = selectRefined(otherMessagePushExtra);
		if(null == dbItem) {
			return false;
		}
		return !otherMessagePushExtra.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherMessagePushExtraMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherMessagePushExtra> otherMessagePushExtras) {
		return otherMessagePushExtraMapper.insertBatch(otherMessagePushExtras);
	}
}
